<template>
  <div class="sidebar-section">
    <h3>商品分类</h3>
    <ul class="category-list">
      <li v-for="category in categories" :key="category.id" class="category-item"
          :class="{'active': category.id === selectedCategory }">
        <a href="javascript:void(0)" @click="selectCategory(category.id)">{{ category.name }}</a>
        <ul v-if="category.children" class="sub-category-list" :class="{ show: category.id === selectedCategory  }"
            :id="category.id">
          <li v-for="subCategory in category.children" :key="subCategory.id" class="sub-category-item">
            <a href="javascript:void(0)" @click="selectSubCategory(subCategory.id)"
               :class="{active:selectedSubCategory===subCategory.id}">{{ subCategory.name }}</a>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>

<script setup>
import {ref, onMounted} from 'vue'
import apis from "@/config/api-config.js";
import getResponse from "@assets/js/http-client.js";
import {ElMessage} from "element-plus";

const emit = defineEmits(['category-selected', 'subcategory-selected'])
const selectedCategory = ref(null)
//二级分类选中状态变量
const selectedSubCategory = ref(null)
const categories = ref([])

const selectCategory = (categoryId) => {
  //如果点击的是当前选中的分类，取消选中
  if (categoryId === selectedCategory.value) {
    selectedCategory.value = null;
    selectedSubCategory.value = null;
    emit('category-selected', null)
    return;
  }
  selectedCategory.value = categoryId
  emit('category-selected', categoryId)
}

const selectSubCategory = (subCategoryId) => {
  //如果点击的是当前选中的二级分类，取消选中
  if (subCategoryId === selectedSubCategory.value) {
    selectedSubCategory.value = null;
    emit('subcategory-selected', null)
    return;
  }
  selectedSubCategory.value = subCategoryId;
  emit('subcategory-selected', subCategoryId)
}
/**
 * 加载商品分类数据
 * @returns {Promise<void>}
 */
const loadCategoryList = async () => {
  let url = apis.product.categoryList.url;
  let type = apis.product.categoryList.type
  const result = await getResponse(url, type);
  if (200 !== result.code) {
    ElMessage.error(result.message);
    return
  }
  categories.value = result.data;
  console.info(categories);
}
onMounted(() => {
  loadCategoryList();
})
</script>

<style scoped>
.sidebar-section {
  margin-bottom: 25px;
  height: 560px;
  overflow-y: auto;
}

.sidebar-section h3 {
  font-size: 18px;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
  color: #212529;
}

.category-list {
  list-style: none;
}

.category-item {
  padding: 8px 0;
}

.category-item a {
  color: #495057;
  text-decoration: none;
  display: block;
  transition: all 0.3s ease;
}

.category-item a:hover {
  color: #007BFF;
  padding-left: 5px;
}

.category-item.active > a {
  color: #007BFF;
  font-weight: bold;
}

/* 二级分类样式 */
.sub-category-list {
  list-style: none;
  padding-left: 20px;
  margin-top: 5px;
  display: none;
}

.sub-category-list.show {
  display: block;
}

.sub-category-item {
  padding: 5px 0;
}

/* 添加二级分类选中样式 */
.sub-category-item .active {
  background-color: #f0f7ff; /* 淡蓝色背景 */
}

.sub-category-item a {
  color: #6c757d;
  font-size: 14px;
  /* 添加内边距使背景色显示更美观 */
  display: block;
  padding: 5px 10px;
  border-radius: 4px;
}

.sub-category-item a:hover {
  color: #007BFF;
}
</style>